<?php

namespace Wentaophp\Yii2Aop\Command;

use Exception;
use Wentaophp\Yii2Aop\Module;
use Wentaophp\Yii2Aop\ProxyServiceProvider;
use yii\console\Controller;
use yii\console\ExitCode;

class ProxyClearCacheCommand extends Controller
{
    /**
     * 指定扫描路径后。扫描注解并生成缓存
     * @throws Exception
     */
    public function actionCache(): int
    {
        if (!\Yii::$app->hasModule(Module::MODULE_ID)) {
            throw new Exception('请先配置 ' . Module::MODULE_ID . ' 模块');
        }
        $aopParams = \Yii::$app->getModule(Module::MODULE_ID)->params;
        if (!isset($aopParams['scanningPath'])) {
            throw new Exception('请先配置扫描路径 scanningPath => [path1, path2,...]');
        }
        $canningPath = $aopParams['scanningPath'];

        echo '开始扫描注解...' . microtime(true) . PHP_EOL;
        (new ProxyServiceProvider())->loadAnnotationAndAspect($canningPath);
        echo '扫描注解完成' . microtime(true) . PHP_EOL;
        return ExitCode::OK;
    }
}
